<template xmlns:el-table="http://www.w3.org/1999/html">
  <div>
    <el-button  type="primary" @click="handleAdd">新增管理员 <i class="el-icon-circle-plus-outline"></i></el-button>
    <el-table :data="tableData">
      <el-table-column prop="usersId" label="管理员id"></el-table-column>
      <el-table-column prop="usersPassword" label="管理员密码"></el-table-column>
      <el-table-column prop="adminName" label="管理员称呼"></el-table-column>
      <el-table-column prop="permissionLevel" label="等级"></el-table-column>
      <el-table-column label="操作"  width="200" align="center">
        <template slot-scope="scope">
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              @confirm="del(scope.row.id)"
          >
            <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>


    <div class="block">
      <span class="demonstration"></span>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[2, 5, 10, 15]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>


    <el-dialog title="新增管理员" :visible.sync="dialogFormVisible" width="30%" >
      <el-button type="success" style="margin-left: 30px" @click="becomeId">生成id</el-button>
      <el-form label-width="100px" size="small" style="margin-top: 15px">
        <el-form-item label="管理员id">
          <el-input v-model="form.usersId" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="管理员密码">
          <el-input v-model="form.usersPassword" autocomplete="off"></el-input>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

export default {
  name: 'HomeView',
  data(){
    return{
      adminName:"",
      permissionLevel:"",
      dialogFormVisible:false,
      adminId:"",
      adminPassword:"",
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 5,
      usersStuName: "",
      usersStuId: "",
      headerBg: 'headerBg',
      form:{
        id:"",
        adminName:"",
        permissionLevel:"",
        usersId:"",
        usersPassword:"",
      }
    }
  },
  created() {
    this.load()
  },
  methods: {
    becomeId(){
      let currentDate = new Date();
      let year = currentDate.getFullYear(); // 年
      let month = currentDate.getMonth() + 1; // 月
      let day = currentDate.getDate(); // 日
      let hours = currentDate.getHours(); // 时
      let minutes = currentDate.getMinutes(); // 分
      let seconds = currentDate.getSeconds(); // 秒
      const id = year+""+month+day+hours+minutes+seconds
      this.form.usersId = id

    },
    del(id) {
      this.request.delete("/Users/adminUser/" + id).then(res => {
        if (res) {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    handleEdit(row) {
      this.form = row
      this.dialogFormVisible = true
    },
    save(){
      this.request.post("/Users/addmember",{
        usersId:this.form.usersId,
        usersPassword:this.form.usersPassword,
        permissionLevel:2,
        adminName:"普通管理员"
      }).then(res=>{
        console.log(res)
        if (res.code==="200"){
          this.$message.success("添加成功")
          this.dialogFormVisible = false;
          this.load()
        }
      }).catch(error =>{
        this.$message.error("添加失败")
      })
    },
    handleAdd(){
      this.dialogFormVisible = true

    },
    load() {
      this.request.get("/Users/admininfo/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
        }
      }).then(res => {
        console.log(res)

        this.tableData = res.data.records
        this.total = res.data.total

      })
    },
    reset() {
      this.load()
    },
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.pageNum = pageNum
      this.load()
    },
  }
}
</script>

